function [status_quo] = func_allocations_serialdict( IJipref,IJipref_choice,IJoutput, IJclassSize,IJVAm1, IJVAm2,IJfracDisadv, IJitidx,IJjtidx,IJappyear,yearselect,makebalanced)



%if firstbest==1
%status_quo=[va_disadv_fb va_adv_fb va_disadv_imp_fb va_adv_imp_fb]; 

%end 

%if firstbest==0
%status_quo=[va_disadv va_adv va_disadv_imp va_adv_imp ]; 

%end 


    %% go down to IJappyear==yearselect
    IJipref=IJipref(IJappyear==yearselect);
    IJipref_choice=IJipref_choice(IJappyear==yearselect);
    IJitidx2015=IJitidx(IJappyear==yearselect);
    IJjtidx2015=IJjtidx(IJappyear==yearselect);
    IJclassSize=IJclassSize(IJappyear==yearselect);
    IJoutput=IJoutput(IJappyear==yearselect);

    
    IJVAm1=IJVAm1(IJappyear==yearselect);
    IJVAm2=IJVAm2(IJappyear==yearselect);
    IJfracDisadv=IJfracDisadv(IJappyear==yearselect);

    
    % redo indexes so that they start at 1 
    
    IJitidx=IJitidx2015-min(IJitidx2015)+1;
    IJjtidx=IJjtidx2015-min(IJjtidx2015)+1;
    
    


   if makebalanced==1
        imax=max(IJitidx);       
        jlist=unique(IJjtidx);
        jkeep=randperm(max(jlist),imax)';  % select a number of classrooms that equals the number of teachers
        jselect=ismember(IJjtidx,jkeep);  % generate a logical vector telling us which to keep
        
        % now apply the selector
        
        IJipref=IJipref( jselect);
        IJipref_choice=IJipref_choice( jselect);       
        IJclassSize=IJclassSize( jselect);
        IJoutput=IJoutput( jselect);
        IJVAm1=IJVAm1( jselect);
        IJVAm2=IJVAm2( jselect);
        IJfracDisadv= IJfracDisadv( jselect);
        IJitidx=IJitidx( jselect);
        IJjtidx= IJjtidx( jselect);


        % sort j in sequential order (to renumber), then reorder everything
        % else
        [b1,Isorter]=sort(IJjtidx); % sort by j
        inbetween=repmat((1:imax),imax,1);
        IJjtidx=inbetween(:);
        
        IJipref=IJipref(Isorter);
        IJipref_choice=IJipref_choice(Isorter);        
        IJclassSize=IJclassSize( Isorter);
        IJoutput=IJoutput( Isorter);
        IJVAm1=IJVAm1( Isorter);
        IJVAm2=IJVAm2(Isorter);
        IJfracDisadv= IJfracDisadv(Isorter);
        IJitidx=IJitidx(Isorter);   
        
     
    end
    
      
    
%% make the teacher preferences rectangular, and then rank

   for i=1:length(IJitidx)
         IJjpref_sq(IJjtidx(i,1),IJitidx(i,1))=IJoutput(i,1);    % these are J X I (useful for DA)
         IJfracDisadv_ij(IJjtidx(i,1),IJitidx(i,1))=IJfracDisadv(i,1);  % these are J x 1 (useful for first best)   
         IJclassSize_ij(IJjtidx(i,1),IJitidx(i,1))=IJclassSize(i,1);  % these are I x J (useful for first best)
         IJijpref_sq(IJitidx(i,1),IJjtidx(i,1))=IJoutput(i,1);    % these are I X J (useful for first best)
         IJclassSize_sq(IJitidx(i,1),IJjtidx(i,1))=IJclassSize(i,1);  % these are I x J (useful for first best)
         IJVAm1_sq(IJitidx(i,1),IJjtidx(i,1))=IJVAm1(i,1);  % these are I X J (useful for first best)
         IJVAm2_sq(IJitidx(i,1),IJjtidx(i,1))=IJVAm2(i,1);  % these are I X J (useful for first best)
         IJfracDisadv_sq(IJitidx(i,1),IJjtidx(i,1))=IJfracDisadv(i,1);  % these are I X J (useful for first best)  
         
         IJipref_sq(IJitidx(i,1),IJjtidx(i,1))=IJipref(i,1); % I x J 
         IJipref_choice_sq(IJitidx(i,1),IJjtidx(i,1))=IJipref_choice(i,1); %   I X J          
            
   end
   
   IJtype1num=(1-IJfracDisadv_sq).*IJclassSize_sq;
   IJtype2num=IJfracDisadv_sq.*IJclassSize_sq;
   

   % generate ranks of teacher preferences
     
    [B1, IJirank]=sort(IJipref_choice_sq,2,'descend');  % this is sorted in descending order; so the first column in I is the most preferred of the teacher in the relevant row 
   
   % generate ranks on VA with disadvantaged
   
   VAdis=IJVAm2_sq(:,1);  % this is now output with advantaged
   [B1, serialdict_order]=sortrows(VAdis,'descend');  % this is sorted in descending order; so the first column in I is the most preferred of the teacher in the relevant row 

     % max each teacher pref (teach max=tm)
    % take first column in IJirank

    jass_iprop_wide_tm=[ (1:length(VAdis))', IJirank(:,1)];   
   
   % serial dictatorship allocation
   
   IJirank_red=IJirank;  %need this alternative for the loop
   for i=1:length(VAdis)
       
      jass_iprop_wide_sd(i,1)=serialdict_order(i,1);
      jass_iprop_wide_sd(i,2)=IJirank_red(serialdict_order(i,1),1);
      target=IJirank_red(serialdict_order(i,1),1);

      % now remove the position from from IJirank
      % difficulty is that it is in a different column in each row

      IJirank_redint=IJirank_red(1,:);
      IJirank_redint(IJirank_redint==target)=[];
      
      for jj=2:length(VAdis)
      IJirank_int=IJirank_red(jj,:);  
      IJirank_int(IJirank_int==target)=[];
      IJirank_redint=[IJirank_redint; IJirank_int];
      end
      
      % update IJirank_red
      IJirank_red=IJirank_redint; 
   end
        
   % solve for output among adv. and disadv. in these two allocations
     [va_disadv_sd, va_adv_sd] = func_output(IJVAm2_sq,IJtype2num,IJVAm1_sq,IJtype1num,jass_iprop_wide_sd); 
     [va_disadv_tm, va_adv_tm] = func_output(IJVAm2_sq,IJtype2num,IJVAm1_sq,IJtype1num,jass_iprop_wide_tm); 

   %compute share of adv./disadv. among matched/unmatched in teacher max
   
   matched=unique(jass_iprop_wide_tm(:,2));
   
      for i=1:length(matched)
          i_type2_stu(i,1)= IJtype2num(1,matched(i,1));         
          i_type1_stu(i,1)= IJtype1num(1,matched(i,1));
      end
        
   disadv_share_matched=sum(i_type2_stu)/(sum(i_type1_stu)+sum(i_type2_stu));
   
   unmatched=(1:length(IJirank))';
   unmatched(matched)=[];
   
  

         for i=1:length(unmatched)
          i_type2_stu_unm(i,1)= IJtype2num(1,unmatched(i,1));         
          i_type1_stu_unm(i,1)= IJtype1num(1,unmatched(i,1));
      end
        
   disadv_share_unmatched=sum(i_type2_stu_unm)/(sum(i_type1_stu_unm)+sum(i_type2_stu_unm));
   
   share_matched=length(matched)/(length(matched)+length(unmatched));
 
 status_quo=[va_disadv_sd va_adv_sd va_disadv_tm va_adv_tm disadv_share_matched disadv_share_unmatched  share_matched];


end

